package com.goldsprite.infinityworld.screens.examples.ui;

import com.badlogic.gdx.graphics.*;
import com.badlogic.gdx.scenes.scene2d.*;
import com.badlogic.gdx.scenes.scene2d.ui.*;
import com.badlogic.gdx.utils.viewport.*;
import com.goldsprite.gdxcore.entities.*;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.g2d.Batch;
import com.goldsprite.gdxcore.logs.LogViewerService;
import com.goldsprite.gdxcore.screens.GScreen;
import com.goldsprite.gdxcore.ui.GWindow;
import com.goldsprite.infinityworld.assets.GlobalAssets;

/*
 创建一个Window作为容器，
 内部填充自定义的GameWorld, 
 gameWorld是包括一个底层gameViewActor和上层gameUILayout的WidgetGroup
 */
public class GameWorldLayoutExampleScreen extends GScreen {
	private Skin skin;
	private Stage editorStage;
	private OrthographicCamera screenCam;
	private FitViewport screenViewport;

	private LogViewerService logViewer;

	float margin = 8;
	float defaultSplitRatio = 0.36f;

	private <T extends Table> void marginLRB(T layout, float margin) {
		layout.padLeft(margin);
		layout.padRight(margin);
		layout.padBottom(margin);
	}

	@Override
	public Viewport getViewport() {
		return screenViewport;
	}


	@Override
	public void create() {
		// 初始化所需资源
		skin = GlobalAssets.getInstance().editorSkin;

		// 设置视口与舞台
		float screenWidthRatio = 1f * Gdx.graphics.getWidth() / Gdx.graphics.getHeight();
		float editorWidth = 960/1f;
		float editorHeight = editorWidth / screenWidthRatio;
		screenCam = new OrthographicCamera();
		screenViewport = new FitViewport(editorWidth, editorHeight, screenCam);
		screenViewport.apply(true);
		editorStage = new Stage(screenViewport);
		getImp().addProcessor(editorStage);

		//游戏视图窗口
		
		//WidgetGroup window = new WidgetGroup();
		GWindow window = new GWindow("Game Window", skin);
		//window.setBackground(ColorTextureUtils.createColorDrawable(Color.SKY));
		window.bottom().left();
		window.setMovable(true);
		window.setResizable(true);
		window.setSize(380, 350);
		window.setPosition(100, 100);
		window.setDebug(true, true);

		//游戏布局容器
		WidgetGroup gameWGroup = new WidgetGroup();
		gameWGroup.addActor(window);
		Container<WidgetGroup> gameWGroupContainer = new Container<>(gameWGroup);
		gameWGroupContainer.bottom().left();
		gameWGroupContainer.minSize(0);
		window.limitTarget = gameWGroupContainer;

		//日志器
		logViewer = new LogViewerService(skin);
		SplitPane consoleSplit = new SplitPane(gameWGroupContainer, logViewer.getRootLayout(), false, skin);
		consoleSplit.setDebug(true, true);
		consoleSplit.setSplitAmount(1f);
		
		//游戏世界Group
		GameWorldGroup gameCanvasGroup = new GameWorldGroup();
		gameCanvasGroup.setScreenCam(editorStage.getCamera());
		window.add(gameCanvasGroup);
		
		//实体世界画布
		GameWorld gameCanvas = new GameWorld() {
			@Override
			public void draw(Batch batch, float parentAlpha) {
				super.draw(batch, parentAlpha);
				Texture avatarTex = GlobalAssets.getInstance().avatarTex;
				batch.draw(avatarTex, 0, 0, screenCam.viewportWidth, screenCam.viewportHeight);
			}
		};
		gameCanvas.setFillParent(true);
		gameCanvasGroup.addActor(gameCanvas);
		gameCanvas.setWorldGroup(gameCanvasGroup);

		//摇杆
		Touchpad touchpad = new Touchpad(0, skin);
		touchpad.setBounds(0, 0, 150, 150);
		gameCanvasGroup.addActor(touchpad);
		

		//总布局
		Table rootTable = new Table();
		rootTable.top().left();
		rootTable.setFillParent(true);

		//增加一个顶部menu
		Table menuTable = new Table(skin);
		menuTable.setBackground("list");
		marginLRB(menuTable, margin/2);
		menuTable.left();

		final float minSplitAmount = 0f;
		//console展开按钮
		{
			TextButton consoleCGBtn = new TextButton("Console", skin);
			marginLRB(consoleCGBtn, margin);
			consoleCGBtn.addListener(new InputListener() {
					boolean dragged;
					boolean expand;
					float oldSplitAmount, defaultSplitAmount = 1 - defaultSplitRatio;

					@Override
					public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) {
						dragged = false;
						return true;
					}

					@Override
					public void touchDragged(InputEvent event, float x, float y, int pointer) {
						dragged = true;
					}

					@Override
					public void touchUp(InputEvent event, float x, float y, int pointer, int button) {
						if (!dragged) {
							if(consoleSplit.getSplitAmount()>=(1-minSplitAmount)) expand = false;
							else expand = true;
							if (expand) {
								oldSplitAmount = consoleSplit.getSplitAmount();
							}
							if (!expand) {
								if (oldSplitAmount == 0)
									oldSplitAmount = defaultSplitAmount;
							}
							expand = !expand;
							float nowSplitAmount = expand ? oldSplitAmount : 1;
							consoleSplit.setSplitAmount(nowSplitAmount);
						}
					}
				});
			menuTable.add(consoleCGBtn);
		}
		rootTable.add(menuTable).expandX().fillX().row();

		// 将 SplitPane 添加到整个布局
		rootTable.add(consoleSplit).expand().fill();
		// 将根表格添加到舞台
		editorStage.addActor(rootTable);
	}

	@Override
	public void render(float delta) {
		super.render(delta);

		editorStage.act(delta);
		editorStage.draw();
	}

}

